package com.atguigu.dga.assessor;

import com.atguigu.dga.governance.bean.AssessParam;
import com.atguigu.dga.governance.bean.GovernanceAssessDetail;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.text.ParseException;
import java.util.Date;

public abstract class Assessor {

    //考评流程控制方法
    public final GovernanceAssessDetail doAssess(AssessParam assessParam ){
        //创建考评结果对象
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        //给考评结果赋值
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setTableName(assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setTableName( assessParam.getTableMetaInfo().getTableName());
        governanceAssessDetail.setSchemaName( assessParam.getTableMetaInfo().getSchemaName());
        governanceAssessDetail.setMetricId( assessParam.getGovernanceMetric().getId()+"");
        governanceAssessDetail.setMetricName( assessParam.getGovernanceMetric().getMetricName());
        governanceAssessDetail.setGovernanceType( assessParam.getGovernanceMetric().getGovernanceType());
        governanceAssessDetail.setTecOwner( assessParam.getTableMetaInfo().getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setCreateTime(new Date());
        //默认先给满分， 在考评器查找问题的过程中，如果有问题，再按照指标的要求重新给分。
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);

        //查找问题
        try {
            checkProblem(governanceAssessDetail,assessParam);
        }catch (Exception e){
            //评分得零分
            governanceAssessDetail.setAssessScore(BigDecimal.ZERO);
            //是否有异常置1
            governanceAssessDetail.setIsAssessException("1");
            //取报错详细信息
            //创建字符串缓冲流
            StringWriter stringWriter = new StringWriter();
            //创建输出对象
            PrintWriter printWriter = new PrintWriter(stringWriter);
            //保存错误信息到字符串缓冲流
            e.printStackTrace(printWriter);

            governanceAssessDetail.setAssessExceptionMsg(
                    stringWriter.toString().substring(0,Math.min(2000,stringWriter.toString().length()))
            );
        }
        return governanceAssessDetail;
    }

    public abstract void checkProblem(GovernanceAssessDetail governanceAssessDetail,AssessParam assessParam) throws Exception;
}
